package com.ruoyi.controller.jpjs.excellence;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.jpjs.common.excellence.Excellence;
import com.ruoyi.domain.jpjs.common.excellence.ExcellenceFile;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.jpjs.excellence.ExcellenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author C.fw
 * @explain 创优创标管理
 * @since 2021/1/21 15:43
 */
@Controller
@RequestMapping("/excellenceManage")
public class ExcellenceManagerController extends BaseController {

    private String prefix = "jpjs/excellence";

    @Autowired
    private ExcellenceService excellenceService;

    /**
     * 危大工程页面
     */
    @GetMapping()
    public String View() {
       return prefix + "/excellenceManage";
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Excellence> util = new ExcelUtil<Excellence>(Excellence.class);
        return util.importTemplateExcel("创优创标计划目标清单");
    }

    @RequestMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file,String type) throws Exception
    {
        ExcellenceFile excellenceFile = new ExcellenceFile();

        // Step1 物理上传
        String fileName = FileUploadUtils.upload(Global.getUploadPath(), file);

        // 文件名,用于下载
        excellenceFile.setFileName(fileName);
        // 虚拟路径,用于展示
        excellenceFile.setVirtualPath(ServerConfig.getUrl() + fileName);
        // 创建人
        excellenceFile.setCreater(ShiroUtils.getSysUser().getLoginName());
        excellenceFile.setType(type);

        // Step2 新增并响应
        return toAjax(excellenceService.insertExcellenceFile(excellenceFile));
    }

    @GetMapping("/selectOfficeFile/{type}")
    public String selectOfficeFile(@PathVariable("type") String type,ModelMap mmap) {
        mmap.put("type", type);
        return prefix + "/excellenceFile";
    }

    @PostMapping("/selectOfficeFileList")
    @ResponseBody
    public TableDataInfo selectOfficeFileList(@RequestParam Map<String, Object> map) {
        startPage();

        // 获取当前登录人对应的项目权限
        List<ExcellenceFile> list = excellenceService.selectOfficeFileList(map);
        return getDataTable(list);
    }

    /**
     * 动态详情页面:删除
     */
    @PostMapping("/removeExcellenceFile")
    @ResponseBody
    public AjaxResult removeExcellenceFile(String ids) {
        try {
            return toAjax(excellenceService.deleteOfficeFileById(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

}
